package com.yantong.service.impl;

import com.yantong.exception.BDException;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.HeadLine;
import com.yantong.pojo.HeadlinePosition;
import com.yantong.pojo.ListHeadlineParams;
import com.yantong.pojo.vo.HeadLineVo;
import com.yantong.mapper.HeadLineMapper;
import com.yantong.service.HeadLineService;
import com.yantong.utils.ValidateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章资讯业务实现类
 * @author sujin
 */
@Service
public class HeadLineServiceImpl implements HeadLineService {

    @Autowired
    private HeadLineMapper headLineMapper;

    /**
     * 分页查询文章资讯列表
     * @param headLineVo
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public PageUtils<HeadLineVo> pageHeadLineList(HeadLineVo headLineVo, int offset, int limit) {
//        Map<String,Object> params = new HashMap<String,Object>(16);
//        if (StringUtils.isNotEmpty(headLineVo.getTitle())) {
//            params.put("title", headLineVo.getTitle());
//        }
//        if (null != headLineVo.getType()) {
//            params.put("type", headLineVo.getType());
//        }
//        if (null != headLineVo.getKeyWords()) {
//            params.put("keyWords", headLineVo.getKeyWords());
//        }
//        if (null != headLineVo.getStatus()) {
//            params.put("status", headLineVo.getStatus());
//        }
        ListHeadlineParams params = new ListHeadlineParams();
        if (StringUtils.isNotEmpty(headLineVo.getTitle())) {
            params.setTitle(headLineVo.getTitle());
        }
        if (null != headLineVo.getCentreId()) {
            params.setCentreId(headLineVo.getCentreId());
        }
        if (null != headLineVo.getType()) {
            params.setType(headLineVo.getType());
        }
        if (null != headLineVo.getKeyWords()) {
            params.setKeyWords(headLineVo.getKeyWords());
        }
        if (null != headLineVo.getStatus()) {
            params.setStatus(headLineVo.getStatus());
        }
        PageUtils<HeadLineVo> pageUtils = new PageUtils<>();
        Integer count = headLineMapper.pageHeadLineListCount(params);
        List<HeadLineVo> headLineList = new ArrayList<>();
        if(count>0){
            headLineList = headLineMapper.pageHeadLineList0(params, new RowBounds(offset, limit));
        }

        pageUtils.setTotal(count);
        pageUtils.setRows(headLineList);
        return pageUtils;
    }

    /**
     * App分页查询文章资讯列表
     * @param headLineVo
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public PageUtils<HeadLineVo> pageHeadLineAppList(HeadLineVo headLineVo, int offset, int limit) {
        Map<String,Object> params = new HashMap<String,Object>(16);
        if (StringUtils.isNotEmpty(headLineVo.getTitle())) {
            params.put("title", headLineVo.getTitle());
        }
        if (null != headLineVo.getMid()) {
            params.put("mid", headLineVo.getMid());
        }
        if (null != headLineVo.getCentreId()) {
            params.put("centreId", headLineVo.getCentreId());
        }
        if (null != headLineVo.getType()) {
            params.put("type", headLineVo.getType());
        }
        if (null != headLineVo.getKeyWords()) {
            params.put("keyWords", headLineVo.getKeyWords());
        }
        PageUtils<HeadLineVo> pageUtils = new PageUtils<>();
        Integer count = headLineMapper.pageHeadLineAppListCount(params);
        List<HeadLineVo> headLineList = new ArrayList<>();
        if(count>0){
            headLineList = headLineMapper.pageHeadLineAppList(params, new RowBounds(offset, limit));
        }
        pageUtils.setTotal(count);
        pageUtils.setRows(headLineList);
        return pageUtils;
    }

    /**
     * 根据id获取文章信息
     * @param id
     * @return
     */
    @Override
    public HeadLineVo getHeadLine(Long id) {
        return headLineMapper.getHeadLine(id);
    }

    /**
     * 文章分类标签名
     * @return
     */
    @Override
    public Map<String, Object> getPositionListToMap(Long centreId) {
        Map<String,Object> map = new HashMap<String, Object>(16);
        Map<String,Object> params = new HashMap<String,Object>(16);
        params.put("centreId",centreId);
        List<HeadlinePosition>  list = headLineMapper.getPositionListToMap(params);
        map.put("headLine", list);
        return map;
    }

    /**
     * 新增文章
     * @param headLine
     * @return
     */
    @Override
    public int addHeadLine(HeadLine headLine) {
        return headLineMapper.insertSelective(headLine);
    }

    /**
     * 修改文章
     * @param headLine
     * @return
     */
    @Override
    public int updateHeadLine(HeadLine headLine) {
        return headLineMapper.updateHeadLine(headLine);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @Override
    public int removeHeadLine(Integer id) {
        return headLineMapper.deleteByPrimaryKey(id);
    }

    /**
     * 获取文章App详情
     * @param id
     * @return
     */
    @Override
    public HeadLineVo getHeadLineDetail(Long id) {
        return headLineMapper.getHeadLineDetail(id);
    }

    /**
     * 阅读数加1
     * @param redNumber
     * @param id
     * @return
     */
    @Override
    public int addReadNumberById(Integer redNumber, Long id) {
        return headLineMapper.addReadNumberById(redNumber, id);
    }

    /**
     * 分页查询文章资讯分类列表
     * @param positionName
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public PageUtils<HeadlinePosition> pageHeadLinePositionList(String positionName, Long centreId, int offset, int limit) {
        Map<String,Object> params = new HashMap<String,Object>(16);
        if (StringUtils.isNotEmpty(positionName)) {
            params.put("positionName", positionName);
        }
        if (ValidateUtil.isNotEmptyObjectOrString(centreId)){
            params.put("centreId",centreId);
        }
        PageUtils<HeadlinePosition> pageUtils = new PageUtils<>();
        List<HeadlinePosition> headlinePositionList = headLineMapper.pageHeadLinePositionList(params, new RowBounds(offset, limit));
        pageUtils.setTotal(headLineMapper.countHeadLinePositionList(params));
        pageUtils.setRows(headlinePositionList);
        return pageUtils;
    }

    /**
     * 新增文章分类
     * @param headlinePosition
     * @return
     */
    @Override
    public int addHeadLinePosition(HeadlinePosition headlinePosition) {
        Map<String,Object> params = new HashMap<String,Object>(16);
        if (ValidateUtil.isNotEmptyObjectOrString(headlinePosition.getCentreId())) {
            params.put("centreId",headlinePosition.getCentreId());
        }
        if (ValidateUtil.isNotEmptyObjectOrString(headlinePosition.getType())){
            params.put("type",headlinePosition.getType());
        }
        Integer num = headLineMapper.pageHeadLinePositionListCount(params);
        if (ValidateUtil.isNotEmptyObjectOrString(num) && num>0){
            throw new BDException("当前类型id已被使用！");
        }
        return headLineMapper.addHeadLinePosition(headlinePosition);
    }

    /**
     * 获取文章分类详情
     * @param id
     * @return
     */
    @Override
    public HeadlinePosition getHeadLinePosition(Long id) {
        return headLineMapper.getHeadLinePosition(id);
    }

    /**
     * 修改文章分类
     * @param headlinePosition
     * @return
     */
    @Override
    public int updateHeadLinePosition(HeadlinePosition headlinePosition) {
        Map<String,Object> params = new HashMap<String,Object>(16);
        if (ValidateUtil.isNotEmptyObjectOrString(headlinePosition.getCentreId())) {
            params.put("centreId",headlinePosition.getCentreId());
        }
        if (ValidateUtil.isNotEmptyObjectOrString(headlinePosition.getType())){
            params.put("type",headlinePosition.getType());
        }
        Integer num = headLineMapper.pageHeadLinePositionListCount(params);
        if (ValidateUtil.isNotEmptyObjectOrString(num) && num>1){
            throw new BDException("当前类型id已被使用！");
        }
        return headLineMapper.updateHeadLinePosition(headlinePosition);
    }

    /**
     * 删除文章分类
     * @param id
     * @return
     */
    @Override
    public int removeHeadLinePosition(Long id) {
        HeadlinePosition headlinePosition = headLineMapper.getHeadLinePosition(id);
        Map<String,Object> params = new HashMap<String,Object>(16);
        if (ValidateUtil.isNotEmptyObjectOrString(headlinePosition.getCentreId())) {
            params.put("centreId",headlinePosition.getCentreId());
        }
        if (ValidateUtil.isNotEmptyObjectOrString(headlinePosition.getType())){
            params.put("type",headlinePosition.getType());
        }
        Integer num = headLineMapper.pageHeadLineAppListCount(params);
        if (ValidateUtil.isNotEmptyObjectOrString(num) && num>0){
            throw new BDException("当前类型id存在对应文章，禁止删除！");
        }
        return headLineMapper.removeHeadLinePosition(id);
    }

}
